package com.imeth.cms.web;

import com.imeth.base.model.Paging;
import com.imeth.base.model.Receipt;
import com.imeth.base.model.Result;
import com.imeth.base.model.Results;
import com.imeth.base.web.BaseController;
import com.imeth.cms.Media;
import com.imeth.cms.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by peter on 12/24/2015.
 */
@Controller
@RequestMapping("/media")
public class MediaController extends BaseController {

    @Autowired
    MediaService mediaService;

    /**
     * 数据查询
     * @param paging
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET)
    public Results<Media> query(@ModelAttribute Paging paging) {
        List<Media> medias = mediaService.query(paging);
        return new Results<Media>(200, "获取数据成功", medias, paging);
    }

    /**
     * 数据获取
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result<Media> getById(@PathVariable Integer id) {
        Media media = mediaService.getById(id);
        return new Result<Media>(200, "获取数据成功", media);
    }

    /**
     * 数据新增
     * @param media
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public Object create(@ModelAttribute Media media, @ModelAttribute Result<Receipt> model) {
        if (!beanValidator(model, media)) {
            return model;
        }
        mediaService.save(media);
        return new Result<Object>(200, "媒体创建成功", media.getId());
    }

    /**
     * 数据更新
     * @param media
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT)
    public Object update(@ModelAttribute Media media, @ModelAttribute Result<Receipt> model) {
        if (!beanValidator(model, media)) {
            return model;
        }

        int resultCount = mediaService.update(media);
        return new Result<Object>(200, "媒体更新成功", resultCount);
    }

    /**
     * 数据删除
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{ids}", method = RequestMethod.DELETE)
    public Result<Object> batchDelete(@PathVariable List<Integer> ids) {
        int resultCount =  mediaService.batchDelete(ids);
        return new Result<Object>(200, "媒体删除成功", resultCount);
    }
}
